﻿// 11. Write a method that adds two polynomials. Represent them as arrays of their coefficients as in the example below:
// 		x2 + 5 = 1x2 + 0x + 5 -> 5  0  1

using System;

class AddPolynomials
{
    static void Main()
    {
        Console.Write("Enter the biggest power of your variable X in your first polynomial : ");
        int power1 = int.Parse(Console.ReadLine());
        double[] array1 = new double[power1 + 1];
        Console.WriteLine("Coefficient for first polynomial");
        Console.WriteLine("Enter the coefficients starting with the one in front of the biggest power of the variable : ");
        for (int i = power1; i >= 0 ; i--)
        {
            Console.Write("Coefficient for power {0} = ", i);
            array1[i] = double.Parse(Console.ReadLine());
        }

        Console.WriteLine();

        Console.Write("Enter the biggest power of your variable X in your second polynomial : ");
        int power2 = int.Parse(Console.ReadLine());
        double[] array2 = new double[power2 + 1];
        Console.WriteLine("Coefficient for second polynomial");
        Console.WriteLine("Enter the coefficients starting with the one in front of the biggest power of the variable : ");
        for (int i = power2; i >= 0; i--)
        {
            Console.Write("Coefficient for power {0} = ", i);
            array2[i] = double.Parse(Console.ReadLine());
        }

        int length = (power1 > power2? power1: power2) + 1;
        double[] resultPolynomialArray = new double[length];
        resultPolynomialArray = AddCoefficients(length, array1, array2);

        Console.WriteLine("The new polynomial is : ");
        for (int i = length - 1; i >= 0; i--)
        {
            Console.WriteLine("Coefficient for power {0} is {1} ", i , resultPolynomialArray[i]);
        }
    }

    static double[] AddCoefficients(int length, double[] array1,double[] array2)
    {
        double[] resultArray = new double[length];
        for (int i = 0; i < length; i++)
        {
            double var1 = 0;
            double var2 = 0;
            if (i >= array1.Length)
            {
                var1 = 0;
            }
            else
            {
                var1 = array1[i];
            }

            if (i >= array2.Length)
            {
                var2 = 0;
            }
            else
            {
                var2 = array2[i];
            }

            resultArray[i] = var1 + var2;
        }

        return resultArray;
    }
}
